// packageD/member/myOrder/refund/components/editAddressPop/editAddressPop.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    popShow:false,
    username: "",
    mobile: "",
    address: "",

    province: "",
    city: "",
    district: "",
    street: "",

    addressName: "",
    districtVal: '',
    strShow: false,
    provinceData: [],
    pickerValue: [0, 0, 0],

    province_id:0,
    city_id:0,
    district_id:0,
    street_id:0
  },

  lifetimes:{
    attached(){
      this._initAddressInfo();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //关闭增加地址
    setPopShow(){
      this.setData({
        popShow:!this.data.popShow
      });
    },
    setFormData(obj){
      let changeData={};
      changeData.street = "";
      changeData.districtVal = "";

      changeData.addressName = obj.province + " " + obj.city + " " + obj.district;

      changeData.province = obj.province;
      changeData.city = obj.city;
      changeData.district = obj.district;

      changeData.street = obj.street;
      changeData.address = obj.address;

      changeData.username = obj.realname;
      changeData.mobile = obj.mobile;

      changeData.province_id = obj.province_id;
      changeData.city_id = obj.city_id;
      changeData.district_id = obj.district_id;
      changeData.street_id = obj.street_id;
      this.setData(changeData);
      this.callback();
      this._getStreet(obj.district_id);
    },
    //地址选择器数据初始化
    _initAddressInfo: function () {
      var that = this;
      var _keyName = 'provinceData';
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          console.log("地址信息准备就绪");
          that._getProvinceData();
        },
        fail: function (res) {
          console.log("省fail");
          app._getNetWork({
            url: app.getNetAddresss("member.member-address.address"),
            success: function (res) {
              var response = res.data;
              var province = response.data.province;
              var city = response.data.city;
              var district = response.data.district;
              wx.setStorage({
                key: 'provinceData',
                data: province
              });
              wx.setStorage({
                key: 'cityData',
                data: city
              });
              wx.setStorage({
                key: 'districtData',
                data: district
              });
              that._initAddressInfo();
            },
            fail: function (res) {},
            state: true
          });
        }
      });
    },
    // 读取省key
    _getProvinceData: function () {
      var that = this;
      var _keyName = 'provinceData';
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          // console.log("省读取成功", res.data);
          that.setData({
            provinceData: res.data
          });
          that.setData({
            "selectAddressData[0].name": res.data[that.data.pickerValue[0]].areaname,
            "selectAddressData[0].id": res.data[that.data.pickerValue[0]].id,
            "selectAddressData[0].index": that.data.pickerValue[0]
          });
          that._getCityData();
        }
      });
    },
    // 读取城市key，筛选城市data
    _getCityData: function () {
      var _data = this.data.selectAddressData[0];
      // console.log("城市", "_data", _data);
      var that = this;
      var _keyName = 'cityData';
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          // console.log("城市读取成功", res);

          var _json = [];
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].parentid == _data.id) {
              _json.push(res.data[i]);
            }
          }
          // console.log("城市数据筛选", _json);
          that.setData({
            cityData: _json
          });
          that.setData({
            "selectAddressData[1].name": _json[that.data.pickerValue[1]].areaname,
            "selectAddressData[1].id": _json[that.data.pickerValue[1]].id,
            "selectAddressData[1].index": that.data.pickerValue[1]
          });
          that._getDistrictData();
        }
      });
    },
    // 读取地区key，筛选地区data
    _getDistrictData: function () {
      var _data = this.data.selectAddressData[1];
      // console.log("地区", "_data", _data)
      var that = this;
      var _keyName = 'districtData';
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          console.log("地区读取成功");
          var _json = [];
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].parentid == _data.id) {
              _json.push(res.data[i]);
            }
          }
          // console.log("地区数据筛选", _json);
          that.setData({
            districtData: _json
          });
          // console.log("districtData", _json);
          that.setData({
            "selectAddressData[2].name": _json[that.data.pickerValue[2]].areaname,
            "selectAddressData[2].id": _json[that.data.pickerValue[2]].id,
            "selectAddressData[2].index": that.data.pickerValue[2]
          });
          // console.log("over", that.data.selectAddressData);
        }
      });
    },
    //打开地址选择器
    openDateLwtbtn: function () {
      this.setData({
        openDateLw: true
      });
    },
    // 关闭选择收货地址
    _closeDateLw: function () {
      this.setData({
        openDateLw: false
      });
    },
    // 收货地址滚动事件
    bindChange: function (e) {
      var val = e.detail.value;
      if (this.data.pickerValue[0] != val[0]) {
        val[1] = 0;
        val[2] = 0;
      }
      if (this.data.pickerValue[1] != val[1]) {
        val[2] = 0;
      }
      this.setData({
        "pickerValue[0]": val[0],
        "pickerValue[1]": val[1],
        "pickerValue[2]": val[2]
      });
      this._initAddressInfo();
    },
    // 确定选择收货地址
    _okAddress: function () {
      let seleData = this.data.selectAddressData;
      this.setData({
        "street": "",
        "districtVal": "",
        "addressName": seleData[0].name + ' ' + seleData[1].name + ' ' + seleData[2].name,
        "province": seleData[0].name,
        "city": seleData[1].name,
        "district": seleData[2].name,
        "province_id": seleData[0].id,
        "city_id": seleData[1].id,
        "district_id": seleData[2].id
      });
      this._getStreet(seleData[2].id);
      this._closeDateLw();
    },
    //获取街道
    _getStreet(param) {
      let that = this;
      let urlStr = app.getNetAddresss("member.memberAddress.street");
      urlStr += "&district_id=" + param;
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1 && !app._isTextEmpty(res.data)) {
            if (!app._isTextEmpty(res.data) && res.data.length > 0) {
              that.setData({
                districtVal: res.data,
                strShow: true
              });
            } else {
              that.setData({
                strShow: false
              });
            }
          } else {
            that.setData({
              strShow: false
            });
          }
        },
        fail: function (res) {
          that.setData({
            strShow: false
          });
          console.log(res);
        }
      });
    },
    streetChoose() {
      if (app._isTextEmpty(this.data.addressName)) {
        wx.showToast({
          icon: 'none',
          title: '请先选择所在地区',
          duration: 1500
        });
      } else {
        this.setData({
          streetShow: true,
          opren: false
        });
      }
    },
    //关闭选择街道弹窗
    streetClose: function () {
      this.setData({
        streetShow: false,
        opren: true
      });
    },
    //选择街道
    streetConfirm(e) {
      let item = e.target.dataset.item;
      this.setData({
        "street": item.areaname,
        "street_id": item.id,
        "streetShow": false,
        opren: true
      });
    },
    appendAddress(){
      if (app._isTextEmpty(this.data.username)) {
        return app.tips('请输入收货人姓名');
      }
      if (app._isTextEmpty(this.data.mobile)) {
        return app.tips('请输入联系电话');
      }
      if (!/^[0-9]{1,}$/.test(this.data.mobile)) {
        return app.tips('请输入联系电话');
      }
      if (app._isTextEmpty(this.data.addressName)) {
        return app.tips('请选择所在区域');
      }
      if (this.data.strShow && app._isTextEmpty(this.data.street)) {
        return app.tips('请选择所在街道');
      }
      if (app._isTextEmpty(this.data.address)) {
        return app.tips('请输入详细地址');
      }
      this.callback();
      this.setPopShow();
    },
    callback(){
      let obj = {
        pick_up_name : this.data.username,
        pick_up_tel : this.data.mobile,
        pick_up_province : this.data.province_id,
        pick_up_city : this.data.city_id,
        pick_up_district : this.data.district_id,
        pick_up_street : this.data.street_id,
        pick_up_address : this.data.address,
        street: this.data.street||'',
        addressName:this.data.addressName
      };
      this.triggerEvent("confirm",obj);
    }
  }
});
